﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Useful.GUI
{
    public class DarkProgressForm : Form, IProgressForm
    {
        private readonly Form specParent;
        private readonly GuiHelper.ProcessActionDelegate progressAction;
        private readonly GuiHelper.ProcessExceptionDelegate processException;
        private readonly string message;
        private ProgressForm form;

        public Exception Error { get; private set; }

        public static void InitForm(Form form, Form parent)
        {
            form.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            form.MaximizeBox = false;
            form.MinimizeBox = false;
            form.ShowIcon = false;
            form.ShowInTaskbar = false;
            form.ControlBox = false;

            form.KeyPreview = true;
        }

        private DarkProgressForm(GuiHelper.ProcessActionDelegate progressAction, GuiHelper.ProcessExceptionDelegate processException, string message, Form parent)
        {
            this.progressAction = progressAction;
            this.processException = processException;
            this.message = message;
            this.specParent = parent;

            InitForm(this, parent);

            KeyDown += ProgressForm_KeyDown;
            Load += ProgressForm_Load;
        }

        private static void ProgressForm_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
        }

        private void ProgressForm_Load(object sender, EventArgs e)
        {
            try
            {
                BackColor = Color.LightGray;
                Opacity = 0.75;

                Bounds = specParent != null ? specParent.Bounds : Screen.PrimaryScreen.Bounds;

                form = new ProgressForm(progressAction, processException, message);
                form.Owner = this;
                form.ShowDialog(this);

                this.Error = form.Error;
            }
            catch (Exception ex)
            {
                if (ex is ProgressGuiException)
                    throw;

                MessageBoxHelper.ShowError(ex);
            }
            finally
            {
                if (form != null)
                    form.Close();

                Close();
            }
        }

        public static void ShowProgress(Form parent, string message, GuiHelper.ProcessActionDelegate progressAction)
        {
            ShowProgress(parent, message, progressAction, null);
        }

        public static void ShowProgress(Form parent, string message, GuiHelper.ProcessActionDelegate progressAction, GuiHelper.ProcessExceptionDelegate processException)
        {
            /*DarkProgressForm form = new DarkProgressForm(progressAction, processException, message, parent);
            form.ShowDialog();

            if (form.Error != null)
            {
                throw new ProgressException(form.Error);
            }*/

            ProgressForm form = new ProgressForm(progressAction, processException, message);
            form.Owner = parent;
            form.ShowDialog(parent);

            if (form.Error != null)
            {
                throw new ProgressGuiException(form.Error);
            }
        }

        #region IProgressForm Members
        public string CurrentProgressText
        {
            get
            {
                return form.CurrentProgressText;
            }
            set
            {
                form.CurrentProgressText = value;
            }
        }

        public TimeSpan GetElapsed()
        {
            return form.GetElapsed();
        }
        #endregion
    }
}
